package de.ret_world.openrpgstudio.editor.helpers;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * This is a helper class for gui internationalization
 *
 * @author Emu
 */
public class Internationalization {

    private Internationalization() {
    }

    /**
     * @return the single instance of the class
     */
    public static Internationalization getInstance() {
        return InternationalizationHolder.INSTANCE;
    }

    private static class InternationalizationHolder {

        private static final Internationalization INSTANCE = new Internationalization();
    }
    private HashMap<String, String> stringtable = new HashMap<>();

    /**
     * Loads a string table from a string-string-map
     * @param table the string table
     */
    public void loadStringTable(Map<String, String> table) {
        stringtable.putAll(table);
    }

    /**
     * Loads a string table from a properties object (this would be the default way to load language data)
     * @param prop the properties object
     */
    public void loadStringTable(Properties prop) {
        for (String k : prop.stringPropertyNames()) {
            stringtable.put(k, prop.getProperty(k));
        }
    }

    /**
     * Gets an internatiolized String. If there is no String bound to the given Key, the output is: $KEY.
     * @param key the language key to find the bound string
     * @return the found string or $KEY
     */
    public String getString(String key) {
        if (stringtable.containsKey(key)) {
            return stringtable.get(key);
        } else {
            return "$" + key;
        }
    }
}
